<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--分类数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="categoryName"
            placeholder="请输入分类名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="categoryOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="过滤条件" prop="key" label-width="100px">
            <!--        TODO key-->
            <el-input
              v-model="queryParams.key"
              placeholder="请输入条件"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryAll">查询全部</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['product:group:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['product:group:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['product:group:remove']"
            >删除
            </el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="分组id" align="center" prop="id"/>
          <el-table-column label="组名" align="center" prop="name"/>
          <el-table-column label="排序" align="center" prop="sort"/>
          <el-table-column label="组图标" align="center" prop="icon"/>
          <el-table-column label="所属分类" align="center" prop="categoryName"/>
          <el-table-column label="备注" align="center" prop="remark"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['product:group:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['product:group:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改属性分组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @close="dialogClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="组名" prop="name">
          <el-input v-model="form.name" placeholder="请输入组名"/>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序"/>
        </el-form-item>
        <el-form-item label="组图标" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入组图标"/>
        </el-form-item>
        <el-form-item label="所属分类" prop="categoryId">
          <!--          <el-input v-model="form.categoryId" placeholder="请输入所属分类id"/>-->
          <el-cascader
            filterable
            clearable
            v-model="values"
            :options="categoryList"
            :props="props"
            @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    关联对话框-->
<!--    <el-dialog :close-on-click-modal="false" :visible.sync="relationVisible" @closed="dialogClose">-->
<!--      <el-dialog width="40%" title="选择属性" :visible.sync="innerVisible" append-to-body>-->
<!--        <div>-->
<!--          <el-form :inline="true" :model="relationForm" @keyup.enter.native="getDataList()">-->
<!--            <el-form-item>-->
<!--              <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>-->
<!--            </el-form-item>-->
<!--            <el-form-item>-->
<!--              <el-button @click="getDataList()">查询</el-button>-->
<!--            </el-form-item>-->
<!--          </el-form>-->
<!--          <el-table-->
<!--            :data="dataList"-->
<!--            border-->
<!--            v-loading="dataListLoading"-->
<!--            @selection-change="innerSelectionChangeHandle"-->
<!--            style="width: 100%;"-->
<!--          >-->
<!--            <el-table-column type="selection" header-align="center" align="center"></el-table-column>-->
<!--            <el-table-column prop="attrId" header-align="center" align="center" label="属性id"></el-table-column>-->
<!--            <el-table-column prop="attrName" header-align="center" align="center" label="属性名"></el-table-column>-->
<!--            <el-table-column prop="icon" header-align="center" align="center" label="属性图标"></el-table-column>-->
<!--            <el-table-column prop="valueSelect" header-align="center" align="center" label="可选值列表"></el-table-column>-->
<!--          </el-table>-->
<!--          <el-pagination-->
<!--            @size-change="sizeChangeHandle"-->
<!--            @current-change="currentChangeHandle"-->
<!--            :current-page="pageIndex"-->
<!--            :page-sizes="[10, 20, 50, 100]"-->
<!--            :page-size="pageSize"-->
<!--            :total="totalPage"-->
<!--            layout="total, sizes, prev, pager, next, jumper"-->
<!--          ></el-pagination>-->
<!--        </div>-->
<!--        <div slot="footer" class="dialog-footer">-->
<!--          <el-button @click="innerVisible = false">取 消</el-button>-->
<!--          <el-button type="primary" @click="submitAddRealtion">确认新增</el-button>-->
<!--        </div>-->
<!--      </el-dialog>-->
<!--      <el-row>-->
<!--        <el-col :span="24">-->
<!--          <el-button type="primary" @click="addRelation">新建关联</el-button>-->
<!--          <el-button-->
<!--            type="danger"-->
<!--            @click="batchDeleteRelation"-->
<!--            :disabled="dataListSelections.length <= 0"-->
<!--          >批量删除</el-button>-->
<!--          &lt;!&ndash;  &ndash;&gt;-->
<!--          <el-table-->
<!--            :data="relationAttrs"-->
<!--            style="width: 100%"-->
<!--            @selection-change="selectionChangeHandle"-->
<!--            border-->
<!--          >-->
<!--            <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->
<!--            <el-table-column prop="attrId" label="#"></el-table-column>-->
<!--            <el-table-column prop="attrName" label="属性名"></el-table-column>-->
<!--            <el-table-column prop="valueSelect" label="可选值">-->
<!--              <template slot-scope="scope">-->
<!--                <el-tooltip placement="top">-->
<!--                  <div slot="content">-->
<!--                    <span v-for="(i,index) in scope.row.valueSelect.split(';')" :key="index">-->
<!--                      {{i}}-->
<!--                      <br />-->
<!--                    </span>-->
<!--                  </div>-->
<!--                  <el-tag>{{scope.row.valueSelect.split(";")[0]+" ..."}}</el-tag>-->
<!--                </el-tooltip>-->
<!--              </template>-->
<!--            </el-table-column>-->
<!--            <el-table-column fixed="right" header-align="center" align="center" label="操作">-->
<!--              <template slot-scope="scope">-->
<!--                <el-button type="text" size="small" @click="relationRemove(scope.row.attrId)">移除</el-button>-->
<!--              </template>-->
<!--            </el-table-column>-->
<!--          </el-table>-->
<!--        </el-col>-->
<!--      </el-row>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
import {listGroup, getGroup, delGroup, addGroup, updateGroup} from "@/api/product/group";
import {listCategory} from "@/api/product/category";

export default {
  name: "AttrGroup",
  //引入的组件必须使用comments注入才能使用
  // comments: {Category},
  data() {
    return {
      //级联选择器数据格式重写
      props: {
        value: "id",
        label: "label",
        children: "children"
      },
      //级联选择器  当前选中的值
      values: [],
      //级联选择器 分类数据
      categoryList: [],

      //分类名称，用来筛选树结构
      categoryName: "",
      //三级分类树数据
      categoryOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 属性分组表格数据
      groupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId: 0,
        key: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getTreeselect();
    this.getList();
  },
  watch: {
    // 根据名称筛选部门树
    categoryName(val) {
      this.$refs.tree.filter(val);
    }

  },
  methods: {
    //查询全部
    handleQueryAll() {
      this.queryParams.categoryId = 0
      this.getList();
    },
    //对话框关闭事件
    dialogClose() {
      this.form.categoryId = 0
      this.values = []
    },
    //级联选择单击事件
    handleChange(value) {
      this.form.categoryId = value[value.length - 1]
      console.log(value);
    },
    /** 查询分类下拉树结构 */
    getTreeselect() {
      listCategory().then(response => {
        console.log("res", response.data)
        this.categoryOptions = response.data;
        this.categoryList = response.data;
      });
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.categoryId = data.id;
      this.handleQuery();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    /** 查询属性分组列表 */
    getList() {
      this.loading = true;
      listGroup(this.queryParams).then(response => {
        this.groupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.form.categoryId = 0
      this.values = []
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sort: null,
        icon: null,
        categoryId: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.queryParams.categoryId === 0) {
        this.$modal.msgError("请先选择所属分类")
        return;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加属性分组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGroup(id).then(response => {
        this.form = response.data;
        this.values = response.data.values;
        this.open = true;
        this.title = "修改属性分组";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGroup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除属性分组编号为"' + ids + '"的数据项？').then(function () {
        return delGroup(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

  }
};
</script>
